package com.gridmobi.xrtb.model.rtb;

import com.gridmobi.xrtb.domain.adn.OsE;
import com.gridmobi.xrtb.domain.dsp.JOffer;
import com.gridmobi.xrtb.handler.DeviceInfo;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.util.Base64Utils;

import java.util.Date;
import java.util.Random;

public class ImpressionMacro {
    public static String getSubid(String geo, String os) {
        return os + "ds" + RandomStringUtils.randomNumeric(1);
    }

    public static String getSubid(JOffer offer, String geo, String os) {
        if (StringUtils.isBlank(offer.getPlacements())) {

            return os + "ds" + RandomStringUtils.randomNumeric(1);
        } else {
            String[] pls = offer.getPlacements().split(",");
            int le = pls.length;
            if (le > 15) {
                le = 15;
            }
            int i = new Random().nextInt(le);
            return pls[i];
        }
    }

    public static String CLICK_SPE = "|";

    public static String getClickId(String SSP, JOffer offer, String geo, String os, DeviceInfo deviceInfo) {

        StringBuilder newCLickid = new StringBuilder("DK" + RandomStringUtils.randomAlphanumeric(2));
        StringBuilder newSec = new StringBuilder();
        newSec.append(DateFormatUtils.format(new Date(), "MMddHHmmss"))
                .append(CLICK_SPE)
                .append(offer.getId())
                .append(CLICK_SPE)
                .append(deviceInfo.getAppid())
                .append(CLICK_SPE)
                .append(deviceInfo.getSsp())
                .append(CLICK_SPE)
                .append(deviceInfo.getPub())
                .append(CLICK_SPE)
                .append(deviceInfo.getGeo())
                .append(CLICK_SPE)
                .append(deviceInfo.getOs());
        String secStr = Base64Utils.encodeToString(newSec.toString().getBytes());
        newCLickid.append(secStr);

        return newCLickid.toString();


    }

    public static String urlReplace(JOffer offer, String geo, String osV, String url, DeviceInfo deviceInfo, String subid, String clickid) {


        if (OsE.iOS.v.equalsIgnoreCase(osV)) {
            if (url.indexOf("{idfa}") > -1 && StringUtils.isNotBlank(deviceInfo.getDevid())) {
                url = StringUtils.replaceAll(url, "\\{idfa}", deviceInfo.getDevid());
            }
        } else if (OsE.Android.v.equalsIgnoreCase(osV)) {
            if (url.indexOf("{gaid}") > -1 && StringUtils.isNotBlank(deviceInfo.getDevid())) {
                url = StringUtils.replaceAll(url, "\\{gaid}", deviceInfo.getDevid());
            }
        }
        if (url.indexOf("{device_id}") > -1 && StringUtils.isNotBlank(deviceInfo.getDevid())) {
            url = StringUtils.replaceAll(url, "\\{device_id}", deviceInfo.getDevid());
        }
        if (url.indexOf("{click_id}") > -1 && StringUtils.isNotBlank(clickid)) {
            url = StringUtils.replaceAll(url, "\\{click_id}", clickid);
        }
        if (url.indexOf("{pub_subid}") > -1 && StringUtils.isNotBlank(subid)) {
            url = StringUtils.replaceAll(url, "\\{pub_subid}", subid);
        }
        if (url.indexOf("{store_appid}") > -1 && StringUtils.isNotBlank(deviceInfo.getAppid())) {
            url = StringUtils.replaceAll(url, "\\{store_appid}", deviceInfo.getAppid());
        }

        if (url.indexOf("appsflyer.com") > 0) {
            if (OsE.iOS.v.equalsIgnoreCase(osV) && url.indexOf("idfa") == -1) {
                url = url + "&idfa=" + deviceInfo.getDevid();
            } else if (OsE.Android.v.equalsIgnoreCase(osV) && url.indexOf("advertising_id") == -1) {
                url = url + "&advertising_id=" + deviceInfo.getDevid();
            }


        } else if (url.indexOf(".adjust.") > 0) {
            if (OsE.iOS.v.equalsIgnoreCase(osV) && url.indexOf("idfa") == -1) {
                if (url.indexOf("campaign=") > 0) {
                    url = url.replace("campaign=", "idfa=" + deviceInfo.getDevid() + "&campaign=");
                } else {
                    url = url + "&idfa=" + deviceInfo.getDevid();
                }
            } else if (OsE.Android.v.equalsIgnoreCase(osV) && url.indexOf("gps_adid") == -1) {
                if (url.indexOf("campaign=") > 0) {
                    url = url.replace("campaign=", "gps_adid=" + deviceInfo.getDevid() + "&campaign=");
                } else {
                    url = url + "&gps_adid=" + deviceInfo.getDevid();
                }


            }

        } else if (url.indexOf(".lazada.") > 0) {
            if (OsE.iOS.v.equalsIgnoreCase(osV) && url.indexOf("idfa") == -1) {
                url = url + "&idfa=" + deviceInfo.getDevid();
            } else if (OsE.Android.v.equalsIgnoreCase(osV) && url.indexOf("gps_adid") == -1) {
                url = url + "&gps_adid=" + deviceInfo.getDevid();
            }


        } else if (url.indexOf(".app.link") > 0) {

            if (OsE.iOS.v.equalsIgnoreCase(osV) && url.indexOf("idfa=") == -1) {
                url = url + "&%24idfa=" + deviceInfo.getDevid();
            } else if (OsE.Android.v.equalsIgnoreCase(osV) && url.indexOf("aaid=") == -1) {
                url = url + "&%24aaid=" + deviceInfo.getDevid();
            }
        }

        return url;

    }

    public static void main(String[] args) {


    }
}
